var gadgetid=document.getElementById("appv0").value;

$("#Main").html(''
				+'<input id="Theme" type="text" width="90px">'
				+'<a href="javascript:void(0);" onclick="addCrazT();">Postar</a>'
				+'<div id="contents"></div>')
 
			var globalViewer ;

			function loadCrazTlist(){
				var idspec = opensocial.newIdSpec({ "userId" : "OWNER", "groupId" : "SELF" });
				var req = opensocial.newDataRequest();
				req.add(req.newFetchPersonRequest("OWNER"), "owner");
				req.add(req.newFetchPersonRequest("VIEWER"), "viewer");
				req.add(req.newFetchPersonAppDataRequest(idspec, ""+gadgetid+""), ""+gadgetid+"");
				req.send(onLoadCrazTlist);
			}
			function addCrazT(){
				var value = $("#Theme").val();
				if (value){
					themes.push(value);
					$("#Theme").val("");
					saveCrazTlist();
				}else{
					alert("Não a nada para postar, escreva algo.")
				}
			}
			function saveCrazTlist(){
				var req = opensocial.newDataRequest();
				var json = gadgets.json.stringify(themes);
				req.add(req.newUpdatePersonAppDataRequest("VIEWER", ''+gadgetid+'', json));
				req.send(onAddCrazT);
			}
			function onAddCrazT(data){
				loadCrazTlist();
			}
			function removeTheme(index,sd){
				$(sd).fadeTo(400, 0.400);
				themes.splice(index,1);
				saveCrazTlist();
			}
			function onLoadCrazTlist(data){
				var owner = data.get("owner").getData();
				var viewer = data.get("viewer").getData();
				globalViewer = viewer;
				try{
					var themelist_json = data.get(""+gadgetid+"").getData()[owner.getId()][''+gadgetid+''];
					var themelist = gadgets.util.unescapeString(themelist_json);
					themes = gadgets.json.parse(themelist)
				}catch(e){
					console.log(e)
					themes = Array();
				}
				var html = Array();
				var qntT=themes.length;
				if (themes.length > 0){
					html.push('<h3>Meus Temas: ('+qntT+')</h3><hr size="1" color="#CCCCCC">');
					
					if (themes.length-1>=0)
					{
					document.getElementById("BgHeader").style.background="#333 url('"+themes[qntT-1]+"') center";
					}

					for (var i=themes.length -1; i >= 0; i--)
					{
						html.push('<div id="ShowT'+i+'" class="photoBG">'
						+'<img src="http://orkut.com/img/castro/trash.gif" title="Remover" class="romvit" onclick="removeTheme(\''+i+'\',\'#ShowT'+i+'\');">'
						+'<div class="PtBorder">'
						+'<a href="'+themes[i]+'" target="_blank"><img src="http://www.orkut.gmodules.com/gadgets/proxy?container=orkut&resize_w=128&resize_h=128&no_expand=1&url='+gadgets.util.escapeString(themes[i])+'"></a>'
						+'</div></div>');
					}

				}else{
					html.push("<i style='font-size:9px;'>nada para mostrar. <b>=(</b></i>")
				}
				$("#contents").html(html.join(""));
				gadgets.window.adjustHeight();

			}
loadCrazTlist();